package com.hqj.client.controller;

import com.hqj.client.entity.BlogsTables;
import com.hqj.client.service.BlogsTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 王建林
 * @version 1.0
 * @date 2020/10/13
 */
@RequestMapping("/blogs")
@Controller
@Api(tags = "博客管理")
public class BlogsTableController {
    @Resource
    private BlogsTableService blogsTableService;

    @ApiOperation("分页查询")
    @GetMapping("/findPage")
    public String findPage(@RequestParam int page,
                           @RequestParam int size, Model model){
        List<BlogsTables> page1 = blogsTableService.findPage(page, size);
        if (page1.isEmpty()){
            model.addAttribute("fycx","查询失败");
            return "main";
        }else {
            model.addAttribute("fycx","查询成功");
            return "main";
        }

    };

    @ApiOperation("删除")
    @DeleteMapping("/delete")
    public String delete (@RequestParam Long id, Model model ){
        int delete = blogsTableService.delete(id);
        if (delete>0){
            model.addAttribute("scbk","删除成功");
        }
        return "main" ;
    }

    @ApiOperation("修改")
    @PutMapping ("/update")
    public String update(@RequestBody BlogsTables blogsTable,Model model){
        int update = blogsTableService.update(blogsTable);
        if (update>0){
            model.addAttribute("xgbk","修改成功");
        }
        return "main";
    }


    @ApiOperation("添加博客")
    @PostMapping("/addOne")
    public String add(@RequestBody BlogsTables blogsTables, Model model){
        int add = blogsTableService.add(blogsTables);
        if (add>0){
           model.addAttribute("zj","添加成功");
        }

        return "main";
    }
    @ApiOperation("查询全部")
    @GetMapping("/findAll")
    public String findAll(Model model){
        List<BlogsTables> all = blogsTableService.findAll();
        if (all.isEmpty()){
            model.addAttribute("cxqb","未查询到");
            return "main";
        }else {
            model.addAttribute("cxqb",all);
            return "main";
        }
    }
    @ApiOperation("根据id查询博客")
    @GetMapping("/selectOne")
    public String selectOne(@RequestParam Long id,Model model){
        BlogsTables blogsTables = blogsTableService.selectOne(id);
        System.out.println(blogsTables);
        if (blogsTables!=null){
            model.addAttribute("msg",blogsTables);
        }
        return "main";
    }
}
